﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.CApExportToPLMXApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToPLMX.Managers;
using DnProcessSimulateCommands.ExportToPLMX.SimulationStateExporter;
using DnProcessSimulateCommands.ExportToPLMX.ViewModels;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX;

public class CApExportToPLMXApp
{
  private CUiETPXOutputViewModel m_outputVm;
  private CUiETPXContentViewModel m_contentVm;

  public CApExportToPLMXApp(CUiETPXOutputViewModel outputVm, CUiETPXContentViewModel contentVm)
  {
    this.m_outputVm = outputVm;
    this.m_contentVm = contentVm;
  }

  public bool ExportStudy()
  {
    if (File.Exists(this.m_outputVm.ExportPath) && TxMessageBox.Show(CCoExportToPLMXUtils.GetResourceManager().GetString("ETPX_OVERWRITE_MSG"), "Export To PLMX", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
      return false;
    bool flag = this.Export();
    if (flag)
    {
      if (this.m_outputVm.LaunchOnCompletion.Value)
        this.RunPLMXFile();
      this.m_outputVm.PersistData();
      this.m_contentVm.PersistData();
    }
    return flag;
  }

  private bool Export()
  {
    CApExportToPlmxFileManager toPlmxFileManager = new CApExportToPlmxFileManager(this.m_outputVm.ExportPath);
    if (!toPlmxFileManager.CreateTempFolder())
      return false;
    try
    {
      Cursor.Current = Cursors.WaitCursor;
      CApExportToPlmxSceneManager plmxSceneManager = new CApExportToPlmxSceneManager(toPlmxFileManager.SceneExportPath);
      plmxSceneManager.ExportScene(toPlmxFileManager.SimulationExportPath, this.m_contentVm.SelectedLOD);
      if (this.m_contentVm.ExportSimulation.Value)
        new CApExportToPlmxStateExportHandler(toPlmxFileManager.SimulationExportPath, plmxSceneManager.ExportedObjectsIds).ExportSimulation(this.m_contentVm.CurrOperation);
      CApExportToPlmxPackageManager plmxPackageManager = new CApExportToPlmxPackageManager(toPlmxFileManager.TempRootExportFolder, this.m_outputVm.PackageName, plmxSceneManager.ExportedObjectsCount);
      plmxPackageManager.CreatStudyPreviewImage();
      plmxPackageManager.CreatePackageInfo();
      new CApExportToPlmxHierarchyManager(toPlmxFileManager.TempRootExportFolder).ExportHierarchy(plmxSceneManager.ExportedObjectsIds);
      toPlmxFileManager.CreatePlmx();
    }
    catch (Exception ex)
    {
      string message = ex.Message;
      CCoExportToPLMXUtils.ShowErrorMessage("ETPX_GENERAL_ERROR");
      return false;
    }
    finally
    {
      Cursor.Current = Cursors.Default;
    }
    return true;
  }

  private void RunPLMXFile() => Process.Start(this.m_outputVm.ExportPath);
}
